<!DOCTYPE html>
<html lang="ch">
<head>
    <meta charset="UTF-8">
    <title>DT-chain: DB</title>
    <script type="text/javascript" src="/static/js/jquery.js"></script>
    <script type="text/javascript" src="/static/js/bootstrap.bundle.min.js"></script>
    <link rel="stylesheet" href="/static/css/bootstrap.min.css">

</head>

<body>
    <div class="container" style="font-size: small; padding: 20px 0;">
        <div class="row" style="padding-bottom: 5px;">
            <div class="col-6 align-self-center">
                Total: <b id="db-num-b"></b>
            </div>
            <div class="col-6 d-grid gap-2 d-md-flex justify-content-md-end">
                <button type="button" class="btn btn-sm btn-outline-danger" onclick="get_clean_up()">清空数据</button>
            </div>
        </div>
        <div class="table-responsive">
<!--        <table class="table table-hover caption-top" style="word-break:break-all;word-wrap:break-word;font-size: small;">-->
        <table class="table table-hover caption-top" style="font-size: small;">
<!--            <caption>Total: <b id="db-num-b"></b></caption>-->
            <thead>
                <tr>
<!--                    <th scope="col">rid</th>-->
                    <th scope="col">height</th>
                    <th scope="col">block_hash</th>
                    <th scope="col">tx_id</th>
                    <th scope="col">tx_type</th>
                    <th scope="col">data_id</th>
                    <th scope="col">business_id</th>
                    <th scope="col">public_key_hash</th>
                    <th scope="col">source_node_address</th>
<!--                    <th scope="col">block_value</th>-->
                </tr>
            </thead>
                <tbody id="db-content-tbody"></tbody>
        </table>
        </div>

    </div>


    <script>

        $(document).ready(function(){
            get_db_data();
        });

        function get_db_data() {
            $.ajax({
                type: "GET",
                timeout : 10000,
                url: "./db_get_data",
                contentType: "application/json; charset=utf-8",
                // data: JSON.stringify(post_msg),
                dataType: "json",
                success: function (message) {
                    if (message.state === 0) {
                        let db_data = message.content;
                        let content_html = '';
                        for(let i in db_data) {
                            content_html += '<tr>';
                            for(let j in db_data[i]) {
                                content_html += '<td>' + db_data[i][j] + '</td>';
                            }
                            content_html += '</tr>';
                        }
                        $('#db-content-tbody').html(content_html);
                        $('#db-num-b').text(''+db_data.length);
                    }
                },
                error: function (message) {}
            });
        }

        function get_clean_up() {

            let confirm_info = confirm('警告：此操作将会删除全部主链数据。');
            if (confirm_info === false) {
                return
            }

            $.ajax({
                type: "GET",
                timeout : 10000,
                url: "./db_clean_up",
                contentType: "application/json; charset=utf-8",
                // data: JSON.stringify(post_msg),
                dataType: "json",
                success: function (message) {
                    if (message.state === 0) {
                        location.reload();
                    }
                },
                error: function (message) {}
            });
        }

    </script>

</body>

</html>